package com.inforich.common.utils;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/27
 */
public class SendSMSUtil {

  private static Logger log = LoggerFactory.getLogger(SendSMSUtil.class);

  private static String accessKeyId = "LTAI5tMTWntjVvbg6AoFkTmA";

  private static String accessKeySecret = "8TnqrZwnlUgfvJ9wndWg70Z813sM73";

  /**
   * 使用AK&SK初始化账号Client
   *
   * @return Client
   * @throws Exception
   */
  private static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
    Config config = new Config()
        // 您的AccessKey ID
        .setAccessKeyId(accessKeyId)
        // 您的AccessKey Secret
        .setAccessKeySecret(accessKeySecret);
    // 访问的域名
    config.endpoint = "dysmsapi.aliyuncs.com";
    return new com.aliyun.dysmsapi20170525.Client(config);
  }

  public static void sendSMS(String phoneNumbers, String sendContent) {
    if (StrUtil.isNotBlank(phoneNumbers)) {
      try {
        // 设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        com.aliyun.dysmsapi20170525.Client client = SendSMSUtil.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
            .setPhoneNumbers(phoneNumbers)
            .setSignName("远康再生")
            .setTemplateCode("SMS_213410329")
            .setTemplateParam("{\"code\":" + sendContent + "}");
        //请求失败这里会抛ClientException异常
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        if (sendSmsResponse.getBody().getCode() != null && sendSmsResponse.getBody().getCode()
            .equals("OK")) {
          log.info("短信发送成功");
        }
      } catch (Exception e) {
        log.error("短信发送失败");
      }
    }
  }
}
